home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / DELAMC.ICN < prev    next >
Text File  |  1992-09-28  |  3KB  |  115 lines

  1. ############################################################################
  2. #
  3. #    File:     delamc.icn
  4. #
  5. #    Subject:  Program to delaminate file using tab characters
  6. #
  7. #    Author:   Thomas R. Hicks
  8. #
  9. #    Date:     May 28, 1989
  10. #
  11. ###########################################################################
  12. #  
  13. #     This program delaminates standard input into several output
  14. #  files according to the separator characters specified by the
  15. #  string following the -t option.  It writes the fields in each
  16. #  line to the corresponding output files as individual lines. If no
  17. #  data occurs in the specified position for a given input line an
  18. #  empty output line is written. This insures that all output files
  19. #  contain the same number of lines as the input file.
  20. #  
  21. #     If - is used as an output file name, the corresponding field
  22. #  is written to the standard output. If the -t option is not used,
  23. #  an ascii horizontal tab character is assumed as the default field
  24. #  separator.
  25. #  
  26. #     The use of delamc is illustrated by the following examples.
  27. #  The command
  28. #  
  29. #          delamc labels opcodes operands
  30. #  
  31. #  writes the fields of standard input, each of which is separated
  32. #  by a tab character, to the output files labels, opcodes, and
  33. #  operands.  The command
  34. #  
  35. #          delamc -t: scores names matric ps1 ps2 ps3
  36. #  
  37. #  writes the fields of standard input, each of which are separated
  38. #  by a colon, to the indicated output files.  The command
  39. #  
  40. #          delamc -t,: oldata f1 f2
  41. #  
  42. #  separates the fields using either a comma or a colon.
  43. #  
  44. ############################################################################
  45. #
  46. #  Links:  usage
  47. #
  48. ############################################################################
  49.  
  50. link usage
  51.  
  52. procedure main(a)
  53.    local tabset, fylist, nxtarg
  54.    if match("-t",a[1]) then {        # tab char given
  55.       tabset := cset(a[1][3:0])
  56.       pop(a)                # get rid of that argument
  57.       }
  58.     if 0 = *(fylist := doutfyls(a)) then
  59.        Usage("delamc [-tc] {outputfile | -} ...")
  60.     /tabset := cset(&ascii[10])            # tab is default separator
  61.     delamrc(tabset,fylist)            # call main routine
  62. end
  63.  
  64. # delamrc - do actual division of input file using tab chars
  65. #
  66. procedure delamrc(tabset,fylist)
  67.     local i, flen, line
  68.     while line := read() do
  69.         {
  70.         i := 1
  71.         flen := *fylist
  72.         line ? while (i <= flen) do
  73.             {
  74.             if i = flen then
  75.                 write(fylist[i][2],tab(0) | "")
  76.             else
  77.                 write(fylist[i][2],tab(upto(tabset)) | tab(0) | "")
  78.             move(1)
  79.             i +:= 1
  80.             }
  81.         }
  82. end
  83.  
  84. # doutfyls - process output file arguments; return list
  85. #
  86. procedure doutfyls(a)
  87.    local lst, x, i
  88.    lst := []
  89.    i := 1
  90.    while \a[i] do {
  91.       if x := llu(a[i],lst) then        # already in list
  92.          lst |||:= [[a[i],lst[x][2]]]
  93.       else                    # not in list
  94.          if a[i] == "-" then            # standard out
  95.             lst |||:= [[a[i],&output]]
  96.          else                # a new file
  97.             if not (x := open(a[i],"w")) then
  98.                stop("Cannot open ",a[i]," for output")
  99.             else lst |||:= [[a[i],x]]
  100.       i +:= 1
  101.       }
  102.    return lst
  103. end
  104.  
  105. # llu - lookup file name in output file list
  106. #
  107. procedure llu(str,lst)
  108.    local i
  109.    i := 1
  110.    while \lst[i] do {
  111.       if \lst[i][1] == str then return i
  112.       i +:= 1
  113.       }
  114. end
  115.